package com.jkoss.wine.warehouse.service.impl;

import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.util.GeneratorNum;
import com.jkoss.wine.merchant.entity.MerchantProductNorm;
import com.jkoss.wine.merchant.mapper.MerchantProductNormMapper;
import com.jkoss.wine.warehouse.entity.WareInventory;
import com.jkoss.wine.warehouse.entity.WareInventoryOrder;
import com.jkoss.wine.warehouse.entity.WareInventoryOrderDetails;
import com.jkoss.wine.warehouse.entity.Warehouse;
import com.jkoss.wine.warehouse.mapper.WareInventoryMapper;
import com.jkoss.wine.warehouse.mapper.WareInventoryOrderDetailsMapper;
import com.jkoss.wine.warehouse.mapper.WareInventoryOrderMapper;
import com.jkoss.wine.warehouse.mapper.WarehouseMapper;
import com.jkoss.wine.warehouse.service.IWareInventoryOrderService;
import com.jkoss.wine.warehouse.vo.SystemInventoryVo;
import com.jkoss.wine.warehouse.vo.WareInventoryOrderVo;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * 仓库盘点单 服务实现类
 * 
 * @Author Jason
 * @Version 1.0, 2018-12-02
 * @See
 * @Since com.jkoss.wine.warehouse.service.impl
 * @Description: TODO
 */
@Service
public class WareInventoryOrderServiceImpl extends ServiceImpl<WareInventoryOrderMapper, WareInventoryOrder> implements IWareInventoryOrderService {

	@Autowired
	private WareInventoryOrderDetailsMapper wareInventoryOrderDetailsMapper;
	@Autowired
	private WarehouseMapper warehouseMapper;
	@Autowired
	private WareInventoryMapper wareInventoryMapper;
	@Autowired
	private MerchantProductNormMapper merchantProductNormMapper;

	@Override
	public Page selectVoByPage(Page page, Wrapper wrapper) {
		SqlHelper.fillWrapper(page, wrapper);
		page.setRecords(baseMapper.selectVoByPage(page, wrapper));
		return page;
	}

	@Override
	public WareInventoryOrderVo selectVoById(String id) {
		return baseMapper.selectVoById(id);
	}

	@Override
	public Page selectVoSystemInventory(Page page, Wrapper wrapper) {
		SqlHelper.fillWrapper(page, wrapper);
		page.setRecords(baseMapper.selectVoSystemInventory(page, wrapper));
		return page;
	}

	@Override
	public Page selectVoSIinventory(Page page, Wrapper wrapper) {
		SqlHelper.fillWrapper(page, wrapper);
		page.setRecords(baseMapper.selectVoSIinventory(page, wrapper));
		return page;
	}

	@Override
	public boolean insertWareInventoryOrder(WareInventoryOrder entity, String wiid[], Integer itat[], Integer psat[]) throws Exception {
		if (!retBool(baseMapper.insert(entity))) {
			throw new Exception("添加盘点单失败");
		}
		List list = new ArrayList();
		for (int i = 0; i < psat.length; i++) {
			WareInventoryOrderDetails details = new WareInventoryOrderDetails();
			// 添加时间
			details.setAddTime(CommonUtil.getDate());
			// 操作人
			details.setOperator(entity.getOperator());
			// 是否删除 0否 1是
			details.setDel(0);
			// 盘点单id
			details.setIoid(entity.getId());
			// 库存id
			details.setWiid(wiid[i]);
			// 盘点数量
			details.setItat(itat[i]);
			// 盈亏数量
			details.setPsat(psat[i]);
			list.add(details);
		}
		if (!retBool(wareInventoryOrderDetailsMapper.insertBatch(list))) {
			throw new Exception("添加盘点单失败");
		}
		return true;
	}

	@Override
	public boolean updateWareInventoryOrder(WareInventoryOrder entity, String wiid[], Integer itat[], Integer psat[]) throws Exception {
		if (!retBool(baseMapper.updateById(entity))) {
			throw new Exception("修改盘点单失败");
		}
		Wrapper wrapper = Condition.create().eq("ioid", entity.getId());
		if (!retBool(wareInventoryOrderDetailsMapper.delete(wrapper))) {
			throw new Exception("修改盘点单失败");
		}
		List list = new ArrayList();
		for (int i = 0; i < psat.length; i++) {
			WareInventoryOrderDetails details = new WareInventoryOrderDetails();
			// 添加时间
			details.setAddTime(CommonUtil.getDate());
			// 操作人
			details.setOperator(entity.getOperator());
			// 是否删除 0否 1是
			details.setDel(0);
			// 盘点单id
			details.setIoid(entity.getId());
			// 库存id
			details.setWiid(wiid[i]);
			// 盘点数量
			details.setItat(itat[i]);
			// 盈亏数量
			details.setPsat(psat[i]);
			list.add(details);
		}
		if (!retBool(wareInventoryOrderDetailsMapper.insertBatch(list))) {
			throw new Exception("修改盘点单失败");
		}
		return true;
	}

	@Override
	public boolean insertExcel(List<SystemInventoryVo> systemInventoryVos, String userid) throws Exception {

		// 为提高效率一次查询所有用户，放到map当中
		Map<String, String> whmap = new HashMap();
		List<Warehouse> warehouses = warehouseMapper.selectList(null);
		if (!CommonUtil.isBlank(warehouses)) {
			for (Warehouse warehouse : warehouses) {
				whmap.put(warehouse.getName(), warehouse.getId());
			}
		}
		// 为提高效率一次查询所有用户，放到map当中
		Map<String, String> mpnmap = new HashMap();
		Wrapper mpnWrapper = Condition.create().ne("del", 1);
		List<MerchantProductNorm> merchantProductNorms = merchantProductNormMapper.selectList(mpnWrapper);
		if (!CommonUtil.isBlank(merchantProductNorms)) {
			for (MerchantProductNorm merchantProductNorm : merchantProductNorms) {
				mpnmap.put(merchantProductNorm.getNormName(), merchantProductNorm.getMerchantProductNormId());
			}
		}

		// 添加盘点单
		WareInventoryOrder inventoryOrder = new WareInventoryOrder();
		// 制单人
		inventoryOrder.setSgps(userid);
		// 制单时间
		inventoryOrder.setOdtm(CommonUtil.getDate());
		// 添加时间
		inventoryOrder.setAddTime(CommonUtil.getDate());
		// 操作人
		inventoryOrder.setOperator(userid);
		// 是否删除 0否 1是
		inventoryOrder.setDel(0);
		// 自动生成订单号
		inventoryOrder.setOnum(GeneratorNum.inventoryOrderNum());

		if (!retBool(baseMapper.insert(inventoryOrder))) {
			throw new Exception("导入盘点单失败");
		}

		List list = new ArrayList();

		for (SystemInventoryVo vo : systemInventoryVos) {
			String pnid = mpnmap.get(vo.getPnname());
			String whid = whmap.get(vo.getWhname());
			if (CommonUtil.isBlank(pnid) || CommonUtil.isBlank(whid)) {
				throw new Exception("导入盘点单失败");
			}

			WareInventoryOrderDetails details = new WareInventoryOrderDetails();
			// 添加时间
			details.setAddTime(CommonUtil.getDate());
			// 操作人
			details.setOperator(userid);
			// 是否删除 0否 1是
			details.setDel(0);

			details.setIoid(inventoryOrder.getId());
			details.setItat(vo.getItat());
			details.setPsat(vo.getPsat());

			WareInventory inventory = new WareInventory();
			inventory.setPnid(pnid);
			inventory.setWhid(whid);
			inventory = wareInventoryMapper.selectOne(inventory);
			if (CommonUtil.isBlank(inventory)) {
				throw new Exception("导入盘点单失败");
			}
			details.setWiid(inventory.getId());
			list.add(details);
		}
		if (!retBool(wareInventoryOrderDetailsMapper.insertBatch(list))) {
			throw new Exception("导入盘点单失败");
		}
		return true;
	}
}
